﻿//-----------------------------------------------------------------------
// <copyright file="AuthorizationServiceBase.Scopes.cs" company="Alan Rodrigues">
//     Copyright (c) Alan Rodrigues. All rights reserved.
// </copyright>
// <developed>Alan Rodrigues (operninha@gmail.com)</developed>
//-----------------------------------------------------------------------

namespace Authorization
{
    using Properties;

    public partial class AuthorizationServiceBase
    {
         #region Scope

        public string[] GetAllScopeNames(string applicationName)
        {
            SecUtility.CheckParameter(ref applicationName, true, true, false, 64, "applicationName");

            var app = AuthorizationStoreManager.GetApplication(applicationName);
            if (app == null)
            {
                throw new AuthorizationException("AuthorizationService.GetAllScopeNames", Resources.invalid_application);
            }
            return app.GetScopeNames();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="applicationName"></param>
        /// <returns></returns>
        public ScopeCollection GetAllScopes(string applicationName)
        {
            SecUtility.CheckParameter(ref applicationName, true, true, false, 64, "applicationName");

            var application = AuthorizationStoreManager.GetApplication(applicationName);
            if (application == null)
            {
                throw new AuthorizationException("AuthorizationService.GetAllScopes", Resources.invalid_application);
            }
            return application.GetScopes();
        }

        public bool ScopeExists(string applicationName, string scopeName)
        {
            SecUtility.CheckParameter(ref applicationName, true, true, false, 64, "applicationName");
            SecUtility.CheckParameter(ref scopeName, true, true, false, 64, "scopeName");
         
            var application = AuthorizationStoreManager.GetApplication(applicationName);
           
            if (application == null)
            {
                throw new AuthorizationException("AuthorizationService.ScopeExists", Resources.invalid_application);
            }

            return application.ScopeExists(scopeName);
        }

        public Scope CreateScope(string applicationName, string scopeName, string description)
        {
            SecUtility.CheckParameter(ref applicationName, true, true, false, 64, "applicationName");
            SecUtility.CheckParameter(ref scopeName, true, true, false, 64, "scopeName");

            var application = AuthorizationStoreManager.GetApplication(applicationName);

            if (application == null)
            {
                throw new AuthorizationException("AuthorizationService.CreateScope", Resources.invalid_application);
            }

            var scope = application.CreateScope(scopeName, description);
            scope.Description = description;
            scope.Save();
            return scope;
        }

        public void DeleteScope(string applicationName, string scopeName)
        {
            SecUtility.CheckParameter(ref applicationName, true, true, false, 64, "applicationName");
            SecUtility.CheckParameter(ref scopeName, true, true, false, 64, "scopeName");

            var application = AuthorizationStoreManager.GetApplication(applicationName);

            if (application == null)
            {
                throw new AuthorizationException("AuthorizationService.DeleteScope", Resources.invalid_application);
            }

            application.DeleteScope(scopeName);
            application.Save();
        }

        public void UpdateScope(string applicationName, string scopeName, string newScopeName, string newDescription)
        {
            SecUtility.CheckParameter(ref applicationName, true, true, false, 64, "applicationName");
            SecUtility.CheckParameter(ref scopeName, true, true, false, 64, "scopeName");
            SecUtility.CheckParameter(ref newScopeName, true, true, false, 64, "newScopeName");
            var application = AuthorizationStoreManager.GetApplication(applicationName);

            if (application == null)
            {
                throw new AuthorizationException("AuthorizationService.UpdateScope", Resources.invalid_application);
            }

            var scope = application.UpdateScope(scopeName, newScopeName, newDescription);
            scope.Save();
        }


        public string[] GetAllAssignedScopesForUser(string applicationName, string userName)
        {
            SecUtility.CheckParameter(ref applicationName, true, true, false, 64, "applicationName");
            SecUtility.CheckParameter(ref userName, false, false, false, 0, "userName");
            return UserAuthorizarionServiceBase.GetAllAssignedScopesForUser(userName, applicationName, null); 
        }

        public AuthorizationResponseWithCursor<string[]> GetAssignedScopesPageForUser(string applicationName, string userName, int pageSize, string cursor)
        {
            SecUtility.CheckParameter(ref applicationName, true, true, false, 64, "applicationName");
            SecUtility.CheckParameter(ref userName, false, false, false, 0, "userName");

            var scopes = UserAuthorizarionServiceBase.GetAssignedScopesPageForUser(userName, applicationName, pageSize, ref cursor);
            return new AuthorizationResponseWithCursor<string[]>(scopes, cursor); 
        }

        public string[] GetAllAssignedScopesForUser(string applicationName, string userName, string filterPattern)
        {
            SecUtility.CheckParameter(ref applicationName, true, true, false, 64, "applicationName");
            SecUtility.CheckParameter(ref userName, false, false, false, 0, "userName");
            return UserAuthorizarionServiceBase.GetAllAssignedScopesForUser(userName, applicationName, filterPattern); 
        }
        
        #endregion
    }
}
